const util = require('../../utils/util.js')
let globalData = getApp().globalData
Page({
  data: {
    isEdit: false,
    productList: null,

    pageIndex: 1,
    loadding: false,
    pullUpOn: true
  },
  onLoad: function(options) {
    if (globalData.userInfo==null) {
      wx.reLaunch({
        url: '/pages/login/login',
      })
    }
    var id= globalData.userInfo.id;
    console.log(id)
    util.request("/app/shopcar2.html", {id:id}, "GET", false,  false ).then((res) => {
      console.log(res)
      this.setData({
        productList: res.data,
        pageIndex: 1,
        pullUpOn: true,
        loadding: false
      })
    })
  },
  changeNum: function(e) {
    console.log(e)
    let value = `productList[${e.detail.index}].num`
    this.setData({
      [value]: e.detail.value
    })
  },
  handlerButton: function(e) {
    console.log(e)
    let index = e.detail.index;
    let item = e.detail.item;

    wx.showToast({
      title: `商品id：${item.id}，按钮index：${index}`,
      icon:"none"
    })
  },
  editGoods: function() {
    this.setData({
      isEdit: !this.data.isEdit
    })
  },
  detail: function() {
    wx.navigateTo({
      url: '../../productDetail/productDetail'
    })
  },
  btnPay() {
    var id= globalData.userInfo.id;
    util.request("/app/pay.html?id="+id,null, "GET", false,  false ).then((res) => {
      console.log(res)
      if (res.msg=="1") {
        util.toast("付款成功",2000, true);
        setTimeout(()=>{
          wx.reLaunch({
            url: '/pages/myOrder/myOrder'
          })
        },2000);
      }
    })
  },
  onPullDownRefresh() {
    setTimeout(() => {
      wx.stopPullDownRefresh()
    }, 200)
  },
  onPullDownRefresh: function() {
    let loadData = JSON.parse(JSON.stringify(this.data.productList));
    loadData = loadData.splice(0, 10)
    this.setData({
      productList: loadData,
      pageIndex:1,
      pullUpOn: true,
      loadding: false
    })
    wx.stopPullDownRefresh()
  },
  onReachBottom: function() {
    if (!this.data.pullUpOn) return;
    this.setData({
      loadding:true
    })
    if (this.data.pageIndex == 4) {
      this.setData({
        loadding:false,
        pullUpOn:false
      })
    } else {
      let loadData = JSON.parse(JSON.stringify(this.data.productList));
      loadData = loadData.splice(0, 10)
      if (this.data.pageIndex == 1) {
        loadData = loadData.reverse();
      }
      this.setData({
        pageIndex: this.data.pageIndex + 1,
        loadding: false,
        productList: this.data.productList.concat(loadData)
      })
    }
  }
})